import 'package:flutter/material.dart';
import 'package:flutterhouse/pages/House/house_detail.dart';
import 'package:flutterhouse/pages/House/house_list.dart';
import 'package:flutterhouse/pages/Location/build_list.dart';
import 'package:flutterhouse/pages/Location/house_form.dart';
import 'package:flutterhouse/pages/Location/location_list.dart';
import 'package:flutterhouse/pages/Location/room_list.dart';
import 'package:flutterhouse/pages/Login/index.dart';
import 'package:flutterhouse/pages/NotFound/not_found.dart';
import 'package:flutterhouse/pages/NoticeDetail/index.dart';
import 'package:flutterhouse/pages/Profile/index.dart';
import 'package:flutterhouse/pages/TabsPage/index.dart';
import 'package:flutterhouse/utils/TokenManager.dart';

void main() {
  runApp(MaterialApp(
    title: '房屋管理系统',
    initialRoute: '/',
    routes: {
      '/': (context) => const TabsPage(),
      '/noticeDetail': (context) => const NoticeDetail(),
      // "/profile": (context) => const ProfilePage(),
      "/login": (context) => const LoginPage(),
    },
    // onGenerateRoute 里面不能有异步，只能是同步
    onGenerateRoute: (setting) {
// 1、获取token
      String token = tokenManager.getToken();
// 2、如果没有token，跳转到登录页面
      if (token.isEmpty) {
        return MaterialPageRoute(
            builder: (context) => LoginPage(toName: setting.name));
      }
      // 3、如果有token，跳转到对应的页面
      if (setting.name == '/profile') {
        return MaterialPageRoute(builder: (context) => const ProfilePage());
      }
      // 我的房屋页面HomeList
      if (setting.name == '/house_list') {
        return MaterialPageRoute(builder: (context) => const HouseList());
      }
      // 添加房屋页面(周边小区)
      if (setting.name == '/LocationList') {
        return MaterialPageRoute(builder: (context) => const LocationList());
      }
      // 7. 小区楼栋
      if (setting.name == "/building_list") {
        return MaterialPageRoute(
          builder: (context) => const BuildingList(),
        );
      }
      // 8. 小区房间
      if (setting.name == "/room_list") {
        return MaterialPageRoute(
          builder: (context) => const RoomList(),
        );
      }
      // 9.小区房间
      if (setting.name == "/house_form") {
        String id = "";
        if (setting.arguments != null) {
          id = (setting.arguments as Map)["id"];
        }
        return MaterialPageRoute(
          // 传递 路由对象
          // settings: setting,
          builder: (context) => HouseForm(id: id),
        );
      }
      // 10. 房屋详情
      if (setting.name == "/house_detail") {
        return MaterialPageRoute(
          settings: setting,
          builder: (context) => const HouseDetail(),
        );
      }
      //  兜底返回 当什么都匹配不了的时候 匹配404页面
      return MaterialPageRoute(
        builder: (context) => const NotFound(),
      );

      // return null;
    },
  ));
}
